package com.jushu.storbox.entities;

import java.util.HashMap;
import java.util.List;

/**
 * any object cached in this Cache, only can use once
 */
public class TempObjectCache {

	HashMap<String, Object> objectMap = new HashMap<String, Object>();
	
	static private TempObjectCache objectCatch = null;
	
	public static TempObjectCache instance() {
		if(objectCatch == null) {
			objectCatch = new TempObjectCache();
		}
		return objectCatch;
	}
	
	public void putObject(String key, Object obj) {
		objectMap.put(key,obj);
	}
	
	public Object getObject(String key) {
		return objectMap.remove(key);
	}
	
	public <T> void putObjectList(String key, List<T> obj) {
		objectMap.put(key, obj);
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getObjectList(String key) {
		List<T> result = null;
		Object tempObj = objectMap.remove(key);
		if(tempObj != null) {
			if(tempObj instanceof List) {
				result = (List<T>)tempObj;
			} else {
				throw new RuntimeException("the object type is not list!");
			}
		}
		return result;
	}
}
